﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Init_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Apollo is loading</title>
    <script src="../js/jquery-1.5.2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        // start 2 threads - one for loading and one for getting the procent

        var maxConcurrentRequest = 2;
        var concurrentRequest = 0;
        var loadedProcent = 0;

        function checkApplicationState() {
            for (var idx = concurrentRequest; idx < maxConcurrentRequest; idx++) {
                setTimeout("requestApplicationState()", idx * 1000);

                concurrentRequest++;
            }
        }

        function requestApplicationState() {

            var currentTime = new Date();
            var ajaxUrl = "/Init/InitHandler.ashx?" + currentTime.getDate() + currentTime.getMilliseconds();

            $.ajax({
                url: ajaxUrl,
                dataType: "json",
                success: function (jsonResponse) {
                    onResponseApplicationState(jsonResponse);
                }
            });

        }

        function onResponseApplicationState(jsonResponse) {
            concurrentRequest--;

            if (jsonResponse.state == "Complete") {
                $("#loading_messages").text("Apollo has loaded... Loading the grafics...");


                document.location.reload(true);
            }
            else if (jsonResponse.state == "Failed") {
                $("#loading_messages").text("Loading failed! Please refresh this page!");
            }
            else { // Pending

                if (loadedProcent < jsonResponse.procent) {
                    loadedProcent = jsonResponse.procent;
                    $("#loading_messages").html(" Apollo is loading...  "
                        + loadedProcent 
                        + " % done. <span><img src='/css/apollo/loading16x16.gif' /></span>");
                }   

                checkApplicationState();
            }

        }

        $(function () {

            checkApplicationState();
            
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div style="width: 300px; height: 100px; vertical-align: middle; margin: auto; border-top: 1px solid #F0F0F0;border-left: 1px solid #F0F0F0;border-right: 2px solid #E0E0E0;border-bottom: 2px solid #E0E0E0; padding: 4px; margin-top: 200px;">
        <img src="/css/apollo/logo150x60.png" />
        <span id="loading_messages" style="display: block; margin-top: 5px; text-align: center"> Apollo is loading... <span><img src='/css/apollo/loading16x16.gif' /></span></span>
    </div>
    </form>
</body>
</html>
